package com.xr.activiti;

import com.xr.activiti.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.activiti.runtime.api.ProcessRuntime;
import org.activiti.runtime.api.TaskRuntime;
import org.activiti.runtime.api.model.ProcessDefinition;
import org.activiti.runtime.api.model.ProcessInstance;
import org.activiti.runtime.api.model.Task;
import org.activiti.runtime.api.model.builders.ProcessPayloadBuilder;
import org.activiti.runtime.api.query.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import org.activiti.runtime.api.query.Pageable;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class SpringActivitiDemo
{
    @Resource
    private ProcessRuntime processRuntime;

    @Resource
    private TaskRuntime taskRuntime;

    @Resource
    private SecurityUtil securityUtil;

    @Test
    public void findProcess()
    {
        securityUtil.logInAs("jack");
        Page<ProcessDefinition> processDefinitionPage = processRuntime.processDefinitions(Pageable.of(0, 10));
        log.info("总数：{}",processDefinitionPage.getTotalItems());
        for (ProcessDefinition processDefinition : processDefinitionPage.getContent())
        {
            log.info("流程定义内容:{}",processDefinition);
        }
    }

    @Test
    public void startProcess()
    {
        securityUtil.logInAs("system");
        ProcessInstance start = processRuntime.start(ProcessPayloadBuilder.start().
                withProcessDefinitionKey("spring-activiti").build());
        log.info("流程实例内容:{}",start);
    }

    @Test
    public void doTask()
    {
        securityUtil.logInAs("other");
        Page<Task> tasks = taskRuntime.tasks(Pageable.of(0, 10));
        if (ObjectUtils.isNotEmpty(tasks) && tasks.getTotalItems() > 0)
        {
            for (Task task : tasks.getContent())
            {

            }
        }
    }
}
